﻿//
//  Monkey Manager
//  Copyright (C) 2012 Victor Tereschenko (aka sibvic)
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ManagerDB
{
    class AutoTagger
    {
        public AutoTagger(AutoTag[] autoTags)
        {
            mAutoTags = autoTags;
        }

        AutoTag[] mAutoTags;

        public class Result
        {
            public List<string> Tags = new List<string>();
            public int Year;
            public int Month;
            public int Day;
        }

        public Result GetTags(string str)
        {
            int year = -1;
            int month = 1;
            int day = 1;
            Result res = new Result();
            foreach (AutoTag tag in mAutoTags)
            {
                Regex r = new Regex(tag.Pattern, RegexOptions.IgnoreCase);
                Match m = r.Match(str);
                if (m.Captures.Count > 0)
                {
                    foreach (string tagToAdd in tag.Tags)
                    {
                        if (!res.Tags.Contains(tagToAdd))
                            res.Tags.AddRange(StoredFileController.GetTags(tagToAdd));
                    }
                    if (m.Groups["year"].Value != "")
                        int.TryParse(m.Groups["year"].Value, out year);
                    if (m.Groups["month"].Value != "")
                        int.TryParse(m.Groups["month"].Value, out month);
                    if (m.Groups["day"].Value != "")
                        int.TryParse(m.Groups["day"].Value, out day);
                }
            }

            if (year > 0 && 1 <= month && month <= 12 && 1 <= day && day <= DateTime.DaysInMonth(year, month))
            {
                res.Year = year;
                res.Month = month;
                res.Day = day;
            }

            return res;
        }

    }
}
